﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Core.Info
{
    /// <summary>
    /// 启动参数信息，当应用启动时，传入的参数，主要用于客户端场景
    /// </summary>
    public class StartArgsInfo
    {
        #region 参数Key
        /// <summary>
        /// 是否调试模式
        /// </summary>
        public static readonly string IsDebug = nameof(IsDebug);

        /// <summary>
        /// 是否开发模式
        /// </summary>
        public static readonly string IsDevelopment = nameof(IsDevelopment);

        /// <summary>
        /// 是否调试语言资源
        /// </summary>
        public static readonly string IsDebugLanguageResource = nameof(IsDebugLanguageResource);

        /// <summary>
        /// 是否输出RestSharpLog - Curl
        /// </summary>
        public static readonly string IsOutputRestSharpLogCurl = nameof(IsOutputRestSharpLogCurl);

        /// <summary>
        /// 是否输出RestSharpLog - Curl
        /// </summary>
        public static readonly string IsOutputRestSharpLog = nameof(IsOutputRestSharpLog);

        /// <summary>
        /// 锁定秒数
        /// </summary>
        public static readonly string LockSeconds = nameof(LockSeconds);

        /// <summary>
        /// 自动登录
        /// </summary>
        public static readonly string AutoLogin = nameof(AutoLogin);

        /// <summary>
        /// 账号
        /// </summary>
        public static readonly string Account = nameof(Account);

        /// <summary>
        /// 密码
        /// </summary>
        public static readonly string Password = nameof(Password);

        /// <summary>
        /// 加密账号
        /// </summary>
        public static readonly string EncryptionAccount = nameof(EncryptionAccount);

        /// <summary>
        /// 加密密码
        /// </summary>
        public static readonly string EncryptionPassword = nameof(EncryptionPassword);
        #endregion 参数Key

        public StartArgsInfo(List<string> startArgs)
        {
            StartArgsOriginal = startArgs;
        }

        /// <summary>
        /// 设置启动参数
        /// </summary>
        /// <param name="argsInfo"></param>
        public void SetStartArgs(List<string> argsInfo)
        {
            StartArgsOriginal = argsInfo;
        }


        private List<string> startArgsOriginal = new List<string>();
        /// <summary>
        /// 启动参数(原始)，当应用启动时，传入的参数，主要用于客户端场景，以"Key:&lt;=&gt;:Value"形式传递,每个参数使用空格分割，当需要传入 " 时，使用\"转义传递
        /// <para>参考："IsDebugLanguageResource:&lt;=&gt;:True" "IsDevelopment:&lt;=&gt;:True" "TestString:&lt;=&gt;:Test \" String Name" "TestString2:&lt;=&gt;:Test 123 \" ,, sd = " </para>
        /// </summary>
        public List<string> StartArgsOriginal
        {
            get => startArgsOriginal;
            set
            {
                startArgsOriginal = value;
                SetStartArgsDictionary();
            }
        }

        /// <summary>
        /// 启动参数(字典)，当应用启动时，传入的参数，主要用于客户端场景 以"Key:&lt;=&gt;:Value"形式传递,每个参数使用空格分割，当需要传入 " 时，使用\"转义传递;
        /// <para>参考："IsDebugLanguageResource:&lt;=&gt;:True" "IsDevelopment:&lt;=&gt;:True" "TestString:&lt;=&gt;:Test \" String Name" "TestString2:&lt;=&gt;:Test 123 \" ,, sd = " </para>
        /// <para>通过设置 <see cref="RunTimeInfo.StartArgsOriginal"/> 参数时，对该变量进行赋值;</para>
        /// <para>Key值相同，将覆盖，Key值大小写敏感</para>
        /// </summary>
        public Dictionary<string, string> StartArgsDictionary { get; private set; } = new Dictionary<string, string>();

        /// <summary>
        /// 从启动参数中获取值
        /// </summary>
        /// <param name="key">转换参数的Key值，大小写敏感</param>
        /// <param name="defaultValue">默认值，获取/转换失败时，返回的默认值</param>
        /// <returns></returns>
        public string Get(string key,string defaultValue = "")
        {
            string res = defaultValue;
            if(StartArgsDictionary?.ContainsKey(key) == true)
            {
                res = StartArgsDictionary[key];
            }
            return res;
        }


        /// <summary>
        /// 从启动参数中获取值
        /// </summary>
        /// <param name="key">转换参数的Key值，大小写敏感</param>
        /// <param name="defaultValue">默认值，获取/转换失败时，返回的默认值</param>
        /// <returns></returns>
        public T? Get<T>(string key, T? defaultValue = default)
        {
            T? res = defaultValue;
            if (StartArgsDictionary?.ContainsKey(key) == true)
            {
                string resString = StartArgsDictionary[key];
                try
                {
                    object resObj = Convert.ChangeType(resString, typeof(T));
                    res = (T)resObj;
                }
                catch(Exception ex)
                {
                    Console.WriteLine($"从启动参数中获取值转换时出错：\r\n{ex.Message}\r\nKey = {key} , Value.ToString() = {resString} , Value.ToType = {typeof(T)}");
                }
            }
            return res;
        }

        /// <summary>
        /// 设置启动参数字典
        /// </summary>
        private void SetStartArgsDictionary()
        {
            StartArgsDictionary = new Dictionary<string, string>();
            if (startArgsOriginal != null)
            {
                foreach (var item in StartArgsOriginal)
                {
                    //分割参数
                    List<string> itemList = item.Split(":<=>:").ToList();
                    string key = "";
                    string value = "";
                    if (itemList.Count >= 1)
                    {
                        key = itemList[0];
                    }
                    if (!string.IsNullOrEmpty(key))
                    {
                        if (itemList.Count > 1)
                        {
                            value = string.Join("", itemList.GetRange(1, itemList.Count() - 1));
                        }
                    }
                    if (!string.IsNullOrEmpty(key))
                    {
                        if (!StartArgsDictionary.ContainsKey(key))
                        {
                            StartArgsDictionary.Add(key, value);
                        }
                        else
                        {
                            StartArgsDictionary[key] = value;
                        }
                    }
                }
            }
        }
    }
}
